package com.sto.stoapp.activity.administrator;

import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.widget.StoHomeActivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class AdminExceptionLogActivity extends StoHomeActivity {
	private TextView exceptionLog;
	private String filePath="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_exception_log);
		
		filePath = getIntent().getStringExtra("FILE_PATH");
		
		
		this.setTitleBottom(R.string.exception_log,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		new BufferThread(this).start();
	}

	private void initWidget() {
		exceptionLog = (TextView) findViewById(R.id.exception_log);
//		File root = Environment.getExternalStorageDirectory();
//		String path = root.getAbsolutePath() + File.separator
//				+ "error_log_sto.txt";// ExceptionLog.LOG_FILE_NAME
		
	}
	
	/**
	 * 
	 * 读取SD卡中文本文件
	 * 
	 * 
	 * 
	 * @param fileName
	 * 
	 * @return
	 */

	private String readSDFile(String fileName) {
		StringBuffer fileContent = new StringBuffer();
		try {
			File f = new File(fileName);
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(f), "utf-8");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while ((line = reader.readLine()) != null) {
					fileContent.append(line);
					fileContent.append("\r\n");
				}
				read.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileContent.toString();
	}
	

	
	/**
	 * 显示数据缓冲线程
	 * 
	 * @author Administrator
	 * 
	 */
	class BufferThread extends BaseThread {

		private Context context;

		public BufferThread(Context context) {
			super(context);
			this.context = context;
		}

		@Override
		public void run() {

			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					exceptionLog.setText(ExceptionLog.getInstance().readLog(new File(filePath)));
				}
			});
		}
	}
	
}
